﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.CC.BaseClass;
using Garlic.CC.Utility;
using Garlic.BLL.Common;
using Garlic.DM.DataModel;
using Garlic.CC.WebUtility;
using Garlic.BLL;
using System.Collections.Generic;
using System.Text;


public partial class SystemManagement_CodeDetailList : BasePage
{
    

    #region page load
    string code_name;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Message = "";
        try
        {
            if (!Page.IsPostBack)
            {
                code_name = Request.QueryString["code_name"];

                if (!(string.IsNullOrEmpty(code_name)))
                {
                    PageInit();
                }
                else
                {
                    this.Message = "页面需要代码名称信息！";
                }                     
            }    
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    private void PageInit()
    {
        //给页面赋初始值
        CodeListInfo codeListInfo = Garlic.BLL.CodeListManager.GetInfo(code_name);
        this.lblCodeName.Text = codeListInfo.Code_name;
        this.lblCdescription.Text = codeListInfo.Code_cdescription;
        this.lblEdescription.Text = codeListInfo.Code_edescription;

        this.SmartGrid.DataSourceID = this.SmartDataViewSource.ID;
        this.SmartDataViewSource.SelectParameters["textWhere"].DefaultValue = " code_name ='" + code_name + "' ";
    }
    #endregion

    #region form view control event handle
    /// <summary>
    /// before inserting data
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void MainFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        try
        {
            this.Validate();

            int userId = UserUtil.GetCurrentUser().UserID;
            DateTime now = DateTime.Now;
            //请修改下面的初始值:
            e.Values["Code_value"] = (e.Values["Code_value"].ToString() == "") ? "初始值" : e.Values["Code_value"];
            e.Values["Value_cdescription"] = (e.Values["Value_cdescription"].ToString() == "") ? string.Empty : e.Values["Value_cdescription"];
            e.Values["Value_edescription"] = (e.Values["Value_edescription"].ToString() == "") ? string.Empty : e.Values["Value_edescription"];
            e.Values["Code_name"] = (e.Values["Code_name"].ToString() == "") ? this.lblCodeName.Text.Trim() : e.Values["Code_name"];
            e.Values["Create_user"] = (e.Values["Create_user"].ToString() == "") ? userId : e.Values["Create_user"];
            e.Values["Create_time"] = (e.Values["Create_time"].ToString() == "") ? now : e.Values["Create_time"];
            e.Values["Modify_user"] = (e.Values["Modify_user"].ToString() == "") ? userId : e.Values["Modify_user"];
            e.Values["Modify_time"] = (e.Values["Modify_time"].ToString() == "") ? now : e.Values["Modify_time"];

        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    /// <summary>
    /// before updating data
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void MainFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        try
        {
            this.Validate();
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
    #endregion


    #region method

    /// <summary>
    /// 初始化查询栏
    /// </summary>
    private void InitSearchBar()
    {

    }


    void InitPartActivity()
    {
    }

    void InitPartType()
    { }


    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }

    private void ExportExcel()
    {
        string filename = "CodeDetail";
        string message;
        bool isSuccess = Garlic.Infrustructure.Utilities.WebUtil.Export(false, SmartGrid, ref filename, out message);
        if (isSuccess)
            OutputFilePath = filename;
        else
            Message = message;

    }


    #endregion

    #region toolbar event handler
    /// <summary>
    /// 工具栏事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
        try
        {
            switch (args.Command)
            {
                case ToolbarCommand.Create:
                    this.MainFormView.DefaultMode = FormViewMode.Insert;
                    SetTopToolBarType("Save");
                    this.SmartGrid.Columns[0].Visible = false;
                    break;
                case ToolbarCommand.Save:
                    this.MainFormView.InsertItem(true);
                    this.SmartGrid.DataBind();
                   
                    break;
                case ToolbarCommand.Export:
                    ExportExcel();
                    break;
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
    #endregion

    protected void SmartGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }

    protected void SmartGrid_DataBound(object sender, EventArgs e)
    {
       
    }
    protected void MainFormView_DataBinding(object sender, EventArgs e)
    {
        
        //code_name = Request.QueryString["code_name"];
        //e.Values["Code_value"] = code_name;
        //e.Values["Code_name"] = code_name;
    }
    protected void MainFormView_DataBound(object sender, EventArgs e)
    {
        
    }
    protected void MainFormView_ItemCreated(object sender, EventArgs e)
    {
        
    }
    protected void MainFormView_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {

    }
    protected void SmartGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        SetTopToolBarType("none");
    }
    protected void SmartGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        SetTopToolBarType("Add");
    }
    protected void SmartGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SetTopToolBarType("Add");
    }

    private void SetTopToolBarType(string type)
    {
        try
        {
            switch (type)
            {
                case "Save":
                    this.TopToolBar.ResetCommandString("保存|Save|save.gif|return SaveFunction();");
                    break;
                case "Add":
                    this.TopToolBar.ResetCommandString("添加|Create|add.gif");
                    break;
                case "none":
                    this.TopToolBar.ResetCommandString("");
                    break;
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
}
